package org.dromara.web.appInterface;

import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.dromara.app.appParams.AppGuiZeParams;
import org.dromara.app.appParams.AppSendCode;
import org.dromara.app.appReturn.AppMyUserInfo;
import org.dromara.app.appReturn.AppProtocolRulesReturn;
import org.dromara.app.config.AppUserActivityConfig;
import org.dromara.app.cssUtils.CssExceptionDescription;
import org.dromara.app.cssUtils.CssUtils;
import org.dromara.app.domain.bo.SkPrivateChatLogBo;
import org.dromara.app.domain.vo.SkAppUserVo;
import org.dromara.app.domain.vo.SkPrivateChatLogVo;
import org.dromara.app.service.IBfAppUserActivityService;
import org.dromara.app.service.ISkAppUserService;
import org.dromara.app.service.ISkPrivateChatLogService;
import org.dromara.app.service.ISkProtocolRulesService;
import org.dromara.common.core.domain.R;
import org.dromara.system.service.ISysConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Tag(name = "其他相关接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/other")
@SaIgnore
public class AppOtherInterface {

    private final ISkProtocolRulesService iSkProtocolRulesService;

    private final ISkAppUserService iSkAppUserService;

    private final ISkPrivateChatLogService skPrivateChatLogService;

    private final ISysConfigService iSysConfigService;

    private final IBfAppUserActivityService iBfAppUserActivityService;

    /**
     * 规则相关接口
     */
    @PostMapping("/protocolRules")
    @Operation(summary = "规则相关接口", method = "post")
    public R<AppProtocolRulesReturn> protocolRules(@RequestBody AppGuiZeParams app){
        AppProtocolRulesReturn apps = iSkProtocolRulesService.protocolRules(app);
        return R.ok(apps);
    }

    /**
     * 订阅用户在线状态
     */

    /**
     * 获取当前城市
     */

    /**
     * 统计注册用户数
     */
    @GetMapping("/getRegisterUserCount")
    @Operation(summary = "统计注册用户数", method = "post")
    public R<Long> getRegisterUserCount(){
        Long count = iSkAppUserService.getRegisterUserCount();
        return R.ok(count);
    }

    /**
     * 扣除私聊次数
     */
    @PostMapping("/deductPrivateChatCount")
    @Operation(summary = "扣除私聊次数", method = "post")
    public R deductPrivateChatCount(@RequestBody SkPrivateChatLogBo chatLogBo, HttpServletRequest req){
        AppMyUserInfo app = iSkAppUserService.getMyInfo(req);
        SkAppUserVo peerUser = iSkAppUserService.queryById(chatLogBo.getPeerId());
        if(peerUser == null){
            CssExceptionDescription.errorMsg(10032);
        }
        if(app.getUserType().equals(1L)){
             if(peerUser.getUserType().equals(1L)){
                 return R.ok();
             }else{
                 if(app.getVipType().equals(0L)){
                     CssExceptionDescription.errorMsg(10031);
                 }

             }
        }
        chatLogBo.setUserId(app.getUserId());
        String num = "0";
        if(app.getVipType().longValue()>0){
            if(app.getVipType().longValue()==1){
                num = iSysConfigService.selectConfigByKey(CssUtils.getHuiYuanConfig("白银每天可以私聊达人的数量").toString());
            }
            if(app.getVipType().longValue()==2){
                num = iSysConfigService.selectConfigByKey(CssUtils.getHuiYuanConfig("黄金每天可以私聊达人的数量").toString());
            }
            if(app.getVipType().longValue()==3){
                num = iSysConfigService.selectConfigByKey(CssUtils.getHuiYuanConfig("钻石每天可以私聊达人的数量").toString());
            }
        }
        SkPrivateChatLogBo privateChatLogBo = new SkPrivateChatLogBo();
        privateChatLogBo.setUserId(app.getUserId());
        privateChatLogBo.setCreateTime(new Date());
        List<SkPrivateChatLogVo> list = skPrivateChatLogService.queryList(chatLogBo);
        if(list.size()>=Integer.parseInt(num)){
            CssExceptionDescription.errorMsg(10030);
        }
        privateChatLogBo.setPeerId(chatLogBo.getPeerId());
        list = skPrivateChatLogService.queryList(privateChatLogBo);
        if(list.isEmpty()){
            skPrivateChatLogService.insertByBo(chatLogBo);
        }
        //添加活跃度
        iBfAppUserActivityService.addActivity(CssUtils.getUserId(req), AppUserActivityConfig.HANDLE_CHAT);
        return R.ok();
    }

}
